package com.framework.admin.service.impl;

import com.framework.admin.service.ILoginService;
import com.framework.admin.service.ISysCompanyService;
import com.framework.common.model.CommonUser;
import com.framework.common.model.UserInfo;
import com.framework.oauthclient.util.OauthClient;
import com.framework.oauthclient.vo.OAuthTokenResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class LoginService implements ILoginService {
    @Autowired
    OauthClient oauthClient;
    @Autowired
    ISysCompanyService sysCompanyService;

    @Override
    public Map<String, Object> doLogin(String userName, String password) {
        // 返回 token 与 用户信息
        OAuthTokenResult ret = oauthClient.getToken(userName, password);
        String token = ret.getAccessToken();
        UserInfo customUser = oauthClient.userInfo(token);
        CommonUser user = customUser.getSysUser();

        // 密码不返回
        user.setPassword(StringUtils.EMPTY);
        String key = String.format("%s %s", "Bearer", token);
        Map<String, Object> authInfo = new HashMap<>(2);
        authInfo.put("token", key);
        authInfo.put("userinfo", customUser);

        log.info("用户登录成功：{}", userName);
        return authInfo;
    }
}
